<template>
	<view class="content">
		<uni-card>
			<view class="sale-card">
				<view class="sale">
					<text class="bold black-color big">日销量</text>
					<text class="black-color big">{{daySale}}</text>
				</view>
				<view class="sale">
					<text class="bold black-color big">日收入</text>
					<text class="black-color big">{{dayRevenue}}</text>
				</view>
				<view class="sale">
					<text class="bold black-color big">月销量</text>
					<text class="black-color big">{{monthSale}}</text>
				</view>
				<view class="sale">
					<text class="bold black-color big">月收入</text>
					<text class="black-color big">{{monthRevenue}}</text>
				</view>
			</view>
		</uni-card>
		
		<u-tabs :list="orderValue" :scrollable="false" lineColor="#296844" @click="tabClick"></u-tabs>
		<view class="tip" v-if="orderList==''">
			<text class="black-color big">还没有订单哦~</text>
		</view>

		<view v-for="(item,index) in orderListIng" v-if="orderTag=='进行中'">
			<uni-card>
				<view class="card">
					<view class="item">
						<!-- 用户名 -->
						<view class="store-pic">
							<image class="store-image" :src="item.user_logo"></image>
						</view>
						<text class="black-color big bold store-name ">{{item.user_name}}</text>
						<!-- 订单状态 -->
						<text class="middle card-status">{{item.status}}</text>
					</view>
					<view class="bag_num">
						<!-- 取袋号 -->
						<view>
							<text class="big black-color bag_text">取袋号:</text>
							<text class="big black-color bold">{{item.bag_num}}</text>
						</view>
						<!-- 取袋时间 -->
						<view>
							<text class="big black-color bag_text">取袋时间:</text>
							<text class="big black-color">{{item.start_time}}-{{item.end_time}}</text>
						</view>
					</view>
					<!-- 商品信息 -->
					<view class="good-item">
						<view class="good_title">
							<view class="good-pic">
								<image class="good-image" :src="item.good_logo"></image>
							</view>
							<text class="middle black-color good-name">{{item.good_name}}</text>
						</view>
						<text class="middle black-color">×{{item.buy_num}}</text>
						<text class="middle black-color">￥{{item.sum_price}}</text>
					</view>
					<!-- 订单时间 -->
					<view class="last">
						<text class="middle order-time">下单时间：{{item.buy_time | formatDate}}</text>
						<u-button class="sub-btn middle black-color" @click="subClick($event,index)">已取袋</u-button>
					</view>
				</view>
			</uni-card>
		</view>

		<view v-for="(item,index) in orderListDone" v-if="orderTag=='已完成'">
			<uni-card>
				<view class="card">
					<view class="item">
						<!-- 用户名 -->
						<view class="store-pic">
							<image class="store-image" :src="item.user_logo"></image>
						</view>
						<text class="black-color big bold store-name ">{{item.user_name}}</text>
						<!-- 订单状态 -->
						<text class="middle card-status">{{item.status}}</text>
					</view>
					<view class="bag_num">
						<!-- 取袋号 -->
						<view>
							<text class="big black-color bag_text">取袋号:</text>
							<text class="big black-color bold">{{item.bag_num}}</text>
						</view>
					</view>
					<!-- 商品信息 -->
					<view class="good-item">
						<view class="good_title">
							<view class="good-pic">
								<image class="good-image" :src="item.good_logo"></image>
							</view>
							<text class="middle black-color good-name">{{item.good_name}}</text>
						</view>

						<text class="middle black-color">×{{item.buy_num}}</text>
						<text class="middle black-color">￥{{item.price*item.buy_num}}</text>
					</view>
					<!-- 时间 -->
					<view class="last">
						<text class="middle order-time">下单时间：{{item.buy_time | formatDate}}</text>
					</view>
					<view>
						<text class="middle order-time">完成时间：{{item.done_time | formatDate}}</text>
					</view>
					<!-- 评价 -->
					<view v-if="item.score==0">
						<text class="middle order-time">买家未评价</text>
					</view>
					<view v-if="item.score>0">
						<text class="middle order-time">买家已评价：{{item.score}}分</text>
					</view>
				</view>
			</uni-card>
		</view>

	</view>
</template>

<script>
	export default {
		data() {
			return {
				store_id: '',
				orderValue: [{
					name: '进行中'
				}, {
					name: '已完成'
				}],
				orderTag: '进行中',
				orderListIng: '',
				orderListDone: '',
				orderList: '',
				sub_show: false,
				daySale: 0,
				dayRevenue: 0,
				monthSale: 0,
				monthRevenue: 0,
			}
		},
		onShow() {
			this.store_id = uni.getStorageSync('storeId');
			this.getOrder();
			this.sum();
		},
		onPullDownRefresh() {
			this.getOrder();
			this.sum();
			//结束下拉刷新
			setTimeout(() => {
				uni.showToast({
					title: '刷新成功',
					icon: 'none'
				});
				uni.stopPullDownRefresh();
			}, 1000);

		},
		methods: {
			getOrder() {
				uniCloud.callFunction({
						name: 'getOrder',
						data: {
							'store_id': this.store_id,
							'status': this.orderTag,
						}
					})
					.then(res => {
						this.orderTag == '进行中' ? this.orderListIng = res.result.data : this.orderListDone = res.result.data;
						this.orderList = res.result.data;
					})
					.catch(error => {
						console.log(error);
					});
			},
			sum() {
				//日销量
				uniCloud.callFunction({
						name: 'countDayOrder',
						data: {
							'store_id': this.store_id,
						}
					})
					.then(res => {
						this.daySale = res.result.data[0].total;
					})
					.catch(error => {
						console.log(error);
					});
				//月销量
				uniCloud.callFunction({
						name: 'countMonthOrder',
						data: {
							'store_id': this.store_id,
						}
					})
					.then(res => {
						this.monthSale = res.result.data[0].total;
					})
					.catch(error => {
						console.log(error);
					});
				//日收入
				uniCloud.callFunction({
						name: 'getDayPrice',
						data: {
							'store_id': this.store_id,
						}
					})
					.then(res => {
						this.dayRevenue = res.result.data[0].sumPrice;
					})
					.catch(error => {
						console.log(error);
					});
				//月收入
				uniCloud.callFunction({
						name: 'getMonthPrice',
						data: {
							'store_id': this.store_id,
						}
					})
					.then(res => {
						this.monthRevenue = res.result.data[0].sumPrice;
					})
					.catch(error => {
						console.log(error);
					});
			},
			tabClick(e) {
				this.orderTag = this.orderValue[e.index].name;
				uniCloud.callFunction({
						name: 'getOrder',
						data: {
							'user_id': getApp().globalData.userId,
							'status': this.orderTag,
						}
					})
					.then(res => {
						this.orderTag == '进行中' ? this.orderListIng = res.result.data : this.orderListDone = res.result
							.data;
						this.orderList = res.result.data;
					})
					.catch(error => {
						console.log(error);
					});
			},
			subClick(e, index) {
				let order_id = this.orderList[index]._id;
				let that = this;
				uni.showModal({
					title: '提示',
					content: '确认商品已取走？',
					success: function(res) {
						if (res.confirm) {
							console.log(order_id);
							uniCloud.callFunction({
									name: 'updateOrder',
									data: {
										'_id': order_id,
										'status': '已完成',
										'done_time': new Date().getTime(),
									}
								})
								.then(res => {
									that.getOrder();
								})
								.catch(error => {
									console.log(error);
								});
						} else {
							// 执行取消后的操作
						}
					}
				})
			}
		},
		//时间戳的处理    
		filters: {
			formatDate: function(value) {
				let date = new Date(value);
				let y = date.getFullYear();
				let MM = date.getMonth() + 1;
				MM = MM < 10 ? ('0' + MM) : MM; //月补0
				let d = date.getDate();
				d = d < 10 ? ('0' + d) : d; //天补0
				let h = date.getHours();
				h = h < 10 ? ('0' + h) : h; //小时补0
				let m = date.getMinutes();
				m = m < 10 ? ('0' + m) : m; //分钟补0
				let s = date.getSeconds();
				s = s < 10 ? ('0' + s) : s; //秒补0
				return y + '-' + MM + '-' + d + ' ' + h + ':' + m + ':' + s; //年月日时分秒
			}

		},
	}
</script>

<style>
	.small {
		font-size: 20rpx;
	}

	.middle {
		font-size: 25rpx;
	}

	.big {
		font-size: 30rpx;
	}

	.bold {
		font-weight: bold;
	}

	.green-color {
		color: #296844;
	}

	.black-color {
		color: black;
	}

	.red-color {
		color: brown;
	}

	.tip {
		height: 100%;
		display: flex;
		justify-content: center;
		align-items: center;
		margin-top: 400rpx;
	}

	.card {
		display: flex;
		flex-direction: column;
	}

	.item {
		display: flex;
		justify-content: center;
		align-items: center;
		flex-direction: row;
	}

	.store-pic {
		flex: 1;
		margin-right: 10rpx;
	}

	.store-image {
		display: block;
		margin: 0 auto;
		width: 30px;
		height: 30px;
		border-radius: 5px;
	}

	.store-name {
		flex: 7;
	}

	.card-status {
		flex: 2;
		text-align: right;
	}

	.bag_num {
		margin: 20rpx 0;
		background-color: rgba(123, 197, 111, 0.3);
		padding: 20rpx;
		border-radius: 5px;
	}

	.bag_text {
		margin-right: 10rpx;
	}

	.good-item {
		display: flex;
		justify-content: space-between;
		align-items: center;
		margin-bottom: 20rpx;
	}

	.good_title {
		display: flex;
		align-items: center;
		flex-direction: row;
	}

	.good-pic {
		flex: 1;
		margin-right: 20rpx;
	}

	.good-image {
		display: block;
		margin: 0 auto;
		width: 50px;
		height: 50px;
		border-radius: 5px;
	}

	.good-name {
		flex: 9;
	}

	.last {
		display: flex;
		flex-direction: row;
		align-items: center;
	}

	.order-time {
		flex: 8;
	}

	.sub-btn {
		flex: 2;
		height: 85%;
		border-radius: 40rpx;
		border: 1px solid #296844;
		background-color: rgba(123, 197, 111, 0.7);
	}

	.sale-card {
		display: flex;
		flex-direction: row;
		justify-content: space-between;
	}

	.sale {
		display: flex;
		flex-direction: column;
		justify-content: center;
		align-items: center;
	}
</style>